<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="服务key" prop="serviceKey">
            <el-input v-model="queryParams.serviceKey" placeholder="请输入服务key" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="服务名称" prop="serviceName">
            <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:role:add']" type="primary" plain icon="Plus" @click="openSelectUser">添加权限</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:role:remove']" type="danger" plain icon="CircleClose" :disabled="multiple" @click="cancelAuthAppRoleMenuAll">
              批量取消授权
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Close" @click="handleClose">关闭</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :search="true" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="menuList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="服务key" align="center" prop="serviceKey" min-width="120" show-overflow-tooltip />
        <el-table-column label="服务名称" align="center" prop="serviceName" min-width="120" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="取消授权" placement="top">
              <el-button v-hasPermi="['system:role:remove']" link type="primary" icon="CircleClose" @click="cancelAuthAppMenu(scope.row)"> </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
      <select-app-menu ref="selectRef" :role-id="queryParams.roleId" @ok="handleQuery" />
    </el-card>
  </div>
</template>

<script setup name="AuthAppMenu" lang="ts">
import {
  allocatedAppRoleMenuList,
  allocatedUserList,
  authAppRoleMenuCancel,
  authAppRoleMenuCancelAll
} from '@/api/system/role';
import { RolemenuVO, RolemenuQuery } from '@/api/system/approleMenu/types';

import SelectAppMenu from './selectAppMenu.vue';
import { RouteLocationNormalized } from 'vue-router';

const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));

const menuList = ref<RolemenuVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const menuIds = ref<Array<string | number>>([]);

const queryFormRef = ref<ElFormInstance>();
const selectRef = ref<InstanceType<typeof SelectAppMenu>>();

const queryParams = reactive<RolemenuQuery>({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId as string,
  serviceKey: undefined,
  serviceName: undefined
});

/** 查询授权用户列表 */
const getList = async () => {
  loading.value = true;
  console.log('queryParams',queryParams)
  console.log('route.params.roleId as string',route.params.roleId as string)
  const res = await allocatedAppRoleMenuList(queryParams);
  menuList.value = res.rows;
  total.value = res.total;
  loading.value = false;


};
// 返回按钮
const handleClose = () => {
  const obj: RouteLocationNormalized = {
    path: '/system/role',
    fullPath: '',
    hash: '',
    matched: [],
    meta: undefined,
    name: undefined,
    params: undefined,
    query: undefined,
    redirectedFrom: undefined
  };
  proxy?.$tab.closeOpenPage(obj);
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
// 多选框选中数据
const handleSelectionChange = (selection: RolemenuVO[]) => {
  menuIds.value = selection.map((item) => item.menuId);
  multiple.value = !selection.length;
};
/** 打开授权用户表弹窗 */
const openSelectUser = () => {
  selectRef.value?.show();
};


/** 取消授权按钮操作 */
const cancelAuthAppMenu = async (row: RolemenuVO) => {
    console.log('menuId',row)
  await proxy?.$modal.confirm('确认要取消该角色"' + row.serviceName + '"菜单吗？');
  //取消授权
  await authAppRoleMenuCancel({ menuId: row.menuId, roleId: queryParams.roleId });
  await getList();
  proxy?.$modal.msgSuccess('取消授权成功');
};
/** 批量取消授权按钮操作 */
const cancelAuthAppRoleMenuAll = async () => {
  const roleId = queryParams.roleId;
  const mIds = menuIds.value.join(',');
  await proxy?.$modal.confirm('是否取消选中菜单授权数据项?');
  await authAppRoleMenuCancelAll({ roleId: roleId, menuIds: mIds });
  await getList();
  proxy?.$modal.msgSuccess('取消授权成功');
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped></style>
